package com.wyx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.constans.Constans;
import com.wyx.dto.DictDataDto;
import com.wyx.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyx.domain.DictData;
import com.wyx.mapper.DictDataMapper;
import com.wyx.service.DictDataService;

/**
 * @author xiaoxiong
 * @version 1.0
 * @date 2021/2/25 21:24
 */
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public DataGridView listPage(DictDataDto dictDataDto) {
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        // 分页
        Page<DictData> page = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
        // 查询选择分类
        qw.eq(StringUtils.isNoneBlank(dictDataDto.getDictType()), DictData.COL_DICT_TYPE, dictDataDto.getDictType());
        // 查询匹配标签查询
        qw.like(StringUtils.isNoneBlank(dictDataDto.getDictLabel()), DictData.COL_DICT_LABEL, dictDataDto.getDictLabel());
        // 查询字典数据状态为0
        qw.eq(StringUtils.isNoneBlank(dictDataDto.getStatus()), DictData.COL_STATUS, Constans.STATUS_TRUE);
        this.dictDataMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public int insert(DictDataDto dictDataDto) {
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataDto, dictData);
        // 设置更新时间以及更新的人
        dictData.setCreateTime(DateUtil.date());
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        return this.dictDataMapper.insert(dictData);
    }

    @Override
    public int update(DictDataDto dictDataDto) {
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataDto, dictData);
        //设置修改人
        dictData.setUpdateBy(dictDataDto.getSimpleUser().getUserName());
        return this.dictDataMapper.updateById(dictData);
    }

    @Override
    public int deleteDictDataByIds(Long[] dictCodeIds) {
        List<Long> longs = Arrays.asList(dictCodeIds);
        if (longs != null && longs.size() > 0) {
            // deleteBatchIds 遍历批量删除
            return this.dictDataMapper.deleteBatchIds(longs);
        } else {
            return -1;
        }
    }

    /**
     * 根据 DictType  查询 DictData
     * 这里是从redis里面查询的
     * @param DictType
     * @return
     */
    @Override
    public List<DictData> selectDictDataByDictType(String DictType) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        String key = Constans.DICT_REDIS_PROFIX + DictType;
        List<DictData> dictDataList = JSON.parseArray(valueOperations.get(key),DictData.class);
        return dictDataList;
    }


    @Override
    public DictData selectDictDataById(Long dictCode) {
        return this.dictDataMapper.selectById(dictCode);
    }
}
